/**
 * Created by DevilsEyes on 2016/3/14.
 */
'use strict';

define(['text!path/orderList/tmp.html?' + cacheControl, 'layer'], function (page, layer) {

    var ORDER_STATUS = {
        ALL: 999,
        ORDER: 0,   //已下单,待支付
        DELIVERY: 30, //已发货,待收货
        CANCEL: -1
    };

    var TAB = [{
        title: "全部订单",
        status: ORDER_STATUS.ALL
    }, {
        title: "待支付",
        status: ORDER_STATUS.ORDER
    }, {
        title: "已发货", status: ORDER_STATUS.DELIVERY
    }, {
        title: "已取消", status: ORDER_STATUS.CANCEL
    }];

    var getStatusStr = function (status) {
        if (status == '0')
            return "待支付";
        if ('10' <= status && status < '30')
            return "待发货";
        if (status >= '30')
            return "已发货";
        if (status == '9')
            return "支付超时";
        if (status == '100')
            return "已完成";
        if (status % 10 == 9)
            return "已取消";

        return "未知状态,请联系客服";
    };

    var render = template.compile(page);

    var apiList = {
        getOrderList: api + 'user/order'
    };

    var vm = new Page.creat({
        $id: 'orderList',
        $model: {
            data: {},
            $: {},
            tabOrderStatus: ORDER_STATUS.ALL,
            init: function (status) {
                vm.$init({
                    setTitle: '我的订单'
                });
                //预渲染
                vm.data = {
                    list: []
                };

                if (status != null)
                    vm.tabOrderStatus = status;
                else
                    vm.tabOrderStatus = ORDER_STATUS.ALL;

                $('#page').html(render(vm.data));

                ex.jsonp({
                    url: apiList.getOrderList,
                    success: function (obj) {
                        if (obj.code != 0)return layer.msg(obj.msg);
                        obj = obj.data;

                        console.log(obj);

                        vm.data = {
                            list: obj,
                            tabs: TAB,
                            select: vm.tabOrderStatus,
                            getStatusStr: getStatusStr
                        };

                        $('#page').html(render(vm.data));

                        ex.async(function () {
                            vm.bindEvent(vm.event);
                            vm.bind$(vm.$);
                        });

                    }
                })
            },
            event: {
                changeTab: function (status) {
                    if (vm.tabOrderStatus != status) {
                        vm.tabOrderStatus = status;


                        var data = {};

                        if (status != 999)
                            data.status = status;

                        ex.jsonp({
                            url: apiList.getOrderList,
                            data: data,
                            success: function (obj) {
                                console.log(obj);
                                if (obj.code != 0)return layer.msg(obj.msg);
                                obj = obj.data;

                                vm.data = {
                                    list: obj,
                                    tabs: TAB,
                                    select: vm.tabOrderStatus,
                                    getStatusStr: getStatusStr
                                };
                                console.log(vm.data.list);

                                $('#page').html(render(vm.data));

                                ex.async(function () {

                                    vm.bindEvent(vm.event);
                                    vm.bind$(vm.$);
                                });

                            }
                        })
                    }
                }

            }
        }
    });


    return {
        init: function (params) {

            vm.init(params.status);
        }


    }
})
;